package com.lsj.juc.myself;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author lsj
 * @create 2021-06-18 10:11
 */
class Ticket2{
    int ticket = 50;

    Lock lock = new ReentrantLock();

    public void sale(){
        lock.lock();
        try {
            if (ticket >0) {
                System.out.println(Thread.currentThread().getName() + "\t" + "卖出第" + (ticket--) + "张\t还剩" + ticket + "张");
            }
        } finally {
            lock.unlock();
        }
    }
}


public class AnyIdeaTest {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(3);
        Ticket2 ticket2 = new Ticket2();
        for (int j = 0; j < 50; j++) {
            pool.submit(()->{ticket2.sale();});
        }

    }
}
